<?php
/**
 * Created by jinqiang on 2024/9/29 11:31.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Soa;


use App\Http\Controllers\Controller;
use App\Http\Repository\KingdeeRepository;
use App\Http\Services\Soa\ReconciliationService;
use Illuminate\Http\Request;

class ReconciliationController extends Controller
{
    //列表
    public function list(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required',
            'type' => 'required'
        ]);
        $param = $request->all();
        $res = $reconciliationService->search($param);
        return result($res);
    }

    //列表--绣花暂估
    public function listXhZg(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required'
        ]);
        $param = $request->all();
        $res = $reconciliationService->searchXhZg($param);
        return result($res);
    }

    //推送--绣花暂估
    public function pushXhZg(Request $request)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $res = KingdeeRepository::pushReconciliationXhZg((Int)$param['id']);
        return $res['code'] === 0 ? success("推送成功") : error($res['msg'], $res);
    }

    //详情--绣花暂估
    public function detailXhZg(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $res = $reconciliationService->detailXhZg((Int)$param['id']);
        return result($res);
    }

    //作废--绣花、成衣
    public function cancel(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $res = $reconciliationService->cancel((Int)$param['id']);
        return $res['code'] === 0 ? success("作废成功") : error($res['msg'], $res);
    }

    //推送--绣花、成衣
    public function push(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'id' => 'required',
            'type' => 'required|in: 0, 1'//0 成衣 1 绣花
        ]);
        $param = $request->all();
        if($param['type'] == 1){
            //推送财务应付--绣花
            $res = KingdeeRepository::pushReconciliationXh((Int)$param['id']);
        }else{
            //推送财务应付--成衣
            $res = KingdeeRepository::pushReconciliationCy((Int)$param['id']);
        }
        return $res['code'] === 0 ? success("推送成功") : error($res['msg'], $res);
    }

    //推送--成衣--违约赔偿
    public function pushFine(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'type' => 'required|in: 0, 1'//0 成衣 1 绣花
        ]);
        $param = $request->all();
        //推送成衣罚款
        $res = KingdeeRepository::pushReconciliationFine((Int)$param['id']);
        return $res['code'] === 0 ? success("推送成功") : error($res['msg'], $res);
    }

    //推送--成衣--额外费用
    public function pushExtra(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'type' => 'required|in: 0, 1'//0 成衣 1 绣花
        ]);
        $param = $request->all();
        //推送成衣额外费用
        $res = KingdeeRepository::pushReconciliationExtra((Int)$param['id']);
        return $res['code'] === 0 ? success("推送成功") : error($res['msg'], $res);
    }

    //付款--绣花、成衣
    public function pay(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'id' => 'required',
            'images' => 'required'
        ]);
        $param = $request->all();
        $res = $reconciliationService->pay((Int)$param['id'], $param['images']);
        return $res['code'] === 0 ? success() : error($res['msg'], $res);
    }

    //审核--绣花、成衣
    public function check(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'id' => 'required',
            'status' => 'required|in:1,2'//1 审核通过 2 审核不通过
        ]);
        $param = $request->all();
        $res = $reconciliationService->check((Int)$param['id'], (Int)$param['status']);
        return $res['code'] === 0 ? success() : error($res['msg'], $res);
    }

    //详情--绣花、成衣
    public function detail(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $res = $reconciliationService->detail((Int)$param['id']);
        return result($res);
    }

    //创建对账单--绣花、成衣
    public function save(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'selectedIds' => 'required',
            'tab' => 'required|in:return,additional,fine',
            'type' => 'required|in:0,1',
            'supplier_id' => 'required',
            'date' => 'required',
            'brand_code' => 'required'
        ]);
        $param = $request->all();
        $res = $reconciliationService->save($param);
        return $res['code'] === 0 ? success("操作成功", $res['data']) : error($res['msg'], $res);
    }

    //确认对账单--绣花、成衣
    public function finish(Request $request, ReconciliationService $reconciliationService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $res = $reconciliationService->finish((Int)$param['id']);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }
}
